/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author menderleit
 */
public class Pizza {

    private int pizzaId;
    private String navn;
    private ArrayList<PizzaSize> sizes;
    private ArrayList<Topping> toppings;

    public Pizza() {
        pizzaId = 0;
        navn = null;
        sizes = new ArrayList();
        toppings = new ArrayList();
    }

    public Pizza(int pizzaID, String navn) {
        this.pizzaId = pizzaID;
        this.navn = navn;
        sizes = new ArrayList();
        toppings = new ArrayList();
    }

    public Pizza(String navn, ArrayList<PizzaSize> sizes, ArrayList<Topping> toppings) {
        pizzaId = 0;
        this.navn = navn;
        this.sizes = sizes;
        this.toppings = toppings;
    }

    public void addSize(int size, int pris) {
        PizzaSize ps = new PizzaSize(size, pris);
        sizes.add(ps);
    }

    public ArrayList<PizzaSize> getSizes() {
        return sizes;
    }

    public void addTopping(Topping t) {
        toppings.add(t);
    }

    public ArrayList<Topping> getToppings() {
        return toppings;
    }

    public String getNavn() {
        return navn;
    }

    public void setNavn(String navn) {
        this.navn = navn;
    }

    public void setPizzaId(int id) {
        pizzaId = id;
    }

    public int getPizzaId() {
        return pizzaId;
    }

    public String pizzaToppings() {
        String pt = "";
        for (int i = 0; i < toppings.size(); i++) {
            pt += toppings.get(i).getNavn();
            if (i < toppings.size() - 1) {
                pt += ", ";
            }
        }
        return pt;
    }

    public static Object[][] pizzaList(DBHandler dbh) throws SQLException {
        ArrayList<Pizza> pizza = dbh.getPizzaList();

        // Find ud af antallet rækker vi skal bruge.
        int numRows = 0;
        for (int i = 0; i < pizza.size(); i++) {
            numRows += pizza.get(i).getSizes().size();
            if (i < pizza.size() - 1) {
                numRows++;
            }
        }
        Object[][] pl = new Object[numRows][3];

        // Opret rækkerne i arrayet.
        int row = 0;
        for (int i = 0; i < pizza.size(); i++) {
            ArrayList<PizzaSize> ps = pizza.get(i).getSizes();
            ArrayList<Topping> t = pizza.get(i).getToppings();
            for (int j = 0; j < ps.size(); j++) {
                if (j == 0) {
                    pl[row][0] = (Pizza) pizza.get(i);
                }
                if (j == 1) {
                    pl[row][0] = (String) pizza.get(i).pizzaToppings();
                }
                pl[row][1] = (String) ps.get(j).toString();
                pl[row][2] = (Integer) ps.get(j).getPris();
                row++;
            }
            if (i < pizza.size() - 1) {
                pl[row][0] = new Spacer();
                row++;
            }
        }

        return pl;
    }

    @Override
    public String toString() {
        return navn;
    }
}
